home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
t_os
/
book
/
src
/
comp_log.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-08
|
3KB
|
114 lines
#include <stdio.h>
#include <stdefs.h>
#include <stdlib.h>
#include <time.h>
const char *compile_log = "comp.log";
struct tm tm;
FILE *pathopen(const char *path, char *mode)
{
FILE *fp;
if ((fp = fopen(path, mode)) == NULL) {
fprintf(stderr, "can't open %s(mode '%s')\n", path, mode);
exit(1);
}
return fp;
}
void read_version(const char *path, int *ver_hi, int *ver_lo)
{
FILE *fp;
fp = pathopen(path, "r");
if (fscanf(fp, "=== Version %d.%d", ver_hi, ver_lo) != 2) {
*ver_hi = *ver_lo = -1;
}
fclose(fp);
}
void write_version(const char *path, int ver_hi, int ver_lo)
{
FILE *fp;
fp = pathopen(path, "w");
fprintf(fp, "=== Version %d.%d === %d/%d/%d %d:%d:%d\n",
ver_hi, ver_lo,
tm.tm_year,tm.tm_mon+1,tm.tm_mday, tm.tm_hour,tm.tm_min,tm.tm_sec);
fclose(fp);
}
void rewrite_c_source(char *path, int ver_hi, int ver_lo)
{
FILE *in, *out;
char *temp = "$$$$temp.$$$";
char buf[256];
int num = 0;
time_t t;
t = time(NULL);
tm = *localtime(&t);
in = pathopen(path, "r");
out = pathopen(temp, "w");
while (fgets(buf,255,in), !feof(in))
{
if (*buf == '#' && num == 0) {
fprintf(out, "#define VERSION \"%d.%d\"\n", ver_hi, ver_lo);
num++;
} else if( *buf == '#' && num == 1) {
fprintf(out, "#define _VERSION \"%d.%d\"\n", ver_hi, ver_lo);
num++;
} else if( *buf == '#' && num == 2) {
strftime(buf, 255, "%b %d %Y", &tm);
fprintf(out, "#define _DATE \"%s\"\n", buf);
num++;
} else if( *buf == '#' && num == 3) {
strftime(buf, 255, "%X", &tm);
fprintf(out, "#define _TIME \"%s\"\n", buf);
num++;
} else {
fputs(buf,out);
}
}
fclose(in);
fclose(out);
remove(path);
rename(temp, path);
}
int main(void)
{
int ver_hi, ver_lo;
/* バージョンナンバーログを読む */
read_version(compile_log, &ver_hi,&ver_lo);
/* まともな番号かどうか調べる */
if (ver_hi < 0 || ver_lo < 0) {
fprintf(stderr, "illegal data in file <%s>\n", compile_log);
exit(1);
}
#if 0
/* バージョンナンバーを 0.001 増加する */
if ((++ver_lo) % 10 == 0)
ver_lo -= 10;
ver_hi += ver_lo / 1000;
ver_lo %= 1000;
#endif
/* バージョン番号と日時をソースファイルに書き込む */
rewrite_c_source("version.c", ver_hi,ver_lo);
printf("version %d.%d\n", ver_hi, ver_lo);
/* ログファイルに書き込んでおく */
write_version(compile_log, ver_hi,ver_lo);
return 0;
}